/*
 * The MIT License 
 *
 * Copyright (c) 2010 pf-miles
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package me.wenyue.jclassview.binview.tree;

import me.wenyue.jclassview.util.Constants;

/**
 * Represents a tree structure of a binary content, it could render a html
 * representation
 * 
 * @author pf-miles 2010-11-16 下午02:07:06
 * 
 */
public class Tree {
    private String id;
    private String styleClass;
    private Node root;

    /**
     * constructor with full params needed
     * 
     * @param id
     *            the id of the rendered html tree
     * @param data
     *            the binary data on which the tree built
     * @param BSDL
     *            the 'binary structure description language'
     * @param styleClass
     *            the css style class of the rendered html tree
     */
    public Tree(String id, byte[] data, String BSDL, String styleClass) {
        // TODO dummy implementation
        this.id = id;
        this.styleClass = styleClass;
        this.root = eval(BSDL, data);
    }

    private Node eval(String BSDL, byte[] data) {
        // TODO dummy impl
        Node[] cs = new Node[2];
        Node[] cs1 = new Node[3];
        cs1[0] = new Leaf("level21", 0, 20);
        cs1[1] = new Leaf("level22", 21, 55);
        cs1[2] = new Leaf("level23", 56, 70);

        cs[0] = new Branch("level11", cs1, 0, 70);

        Node[] cs2 = new Node[2];
        Node[] cs3 = new Node[2];
        cs3[0] = new Leaf("level31", 71, 90);
        cs3[1] = new Leaf("level32", 91, 100);
        cs2[0] = new Branch("level21", cs3, 71, 100);
        cs2[1] = new Leaf("level22", 101, 128);
        cs[1] = new Branch("level12", cs2, 71, 128);
        Branch root = new Branch("all", cs, 0, 128);
        return root;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder("<ul id=\"");
        sb.append(this.id).append("\" class=\"");
        sb.append(this.styleClass).append("\">").append(Constants.NEW_LINE);
        sb.append(root.renderHtml());
        sb.append("</ul>").append(Constants.NEW_LINE);
        return sb.toString();
    }

    // public static void main(String... args) {
    // System.out.println(new Tree("tree", new byte[0], "blahblahblah",
    // "treeview-red"));
    // }
}
